using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Model.Page;
using Keep.Service;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class VideoController : ControllerBase
{
    public class VideoRequest
    {
        public string? path { get; set; }
        public string? oldname { get; set; }
        public string? newname { get; set; }
        public string? name { get; set; }
        public string? keypath { get; set; }
    }

    private readonly IFileService fileService;

    public VideoController(IFileService fileService)
    {
        this.fileService = fileService;
    }

    [HttpPost(Name = "UploadVideo")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadVideo(List<IFormFile> file, string? fileurl)
    {
        try
        {
            if (file.Count > 0)
            {
                if (String.IsNullOrEmpty(fileurl))
                {
                    fileurl = Request.Form["fileurl"];
                }
                Log.Warning("UploadVideo => " + fileurl);
                fileService.UploadFile(file, fileurl, Static.VideoPath);
                return Ok(new ResultData(Static.Enable, file[0].FileName));
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }

    [HttpPost(Name = "DelVideo")]
    public async Task<ActionResult<ResultData>> DelVideo(VideoRequest VideoRequest)
    {
        Log.Warning("IN =>   path=" + VideoRequest.path + "  name=" + VideoRequest.name);
        bool sys = await Task.Run(() => fileService.DelFile(VideoRequest.path, Static.VideoPath, VideoRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpGet(Name = "GetVideoPathInfo")]
    public async Task<ActionResult<ResultData>> GetVideoPathInfo(string? path, int currentPage, int pageSize)
    {
        Log.Warning("IN => " + path);
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetPathInfo(Static.VideoPath, path, currentPage, pageSize));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(Static.Enable, sys));
    }
    [HttpPost(Name = "AddVideoDirectory")]
    public async Task<ActionResult<ResultData>> AddVideoDirectory(VideoRequest VideoRequest)
    {
        Log.Warning("IN => " + VideoRequest.path);
        string sys = await Task.Run(() => fileService.AddFileDirectory(VideoRequest.path, Static.VideoPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelVideoDir")]
    public async Task<ActionResult<ResultData>> DelVideoDir(VideoRequest VideoRequest)
    {
        Log.Warning("IN =>   path=" + VideoRequest.path + "  name=" + VideoRequest.name);
        bool sys = await Task.Run(() => fileService.DelFileDir(VideoRequest.path, Static.VideoPath, VideoRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "EditVideoDirOrName")]
    public async Task<ActionResult<ResultData>> EditVideoDirOrName(VideoRequest VideoRequest)
    {
        Log.Warning("IN => path=" + VideoRequest.path + "    oldname=" + VideoRequest.oldname + "    newname=" + VideoRequest.newname);
        bool sys = await Task.Run(() => fileService.EditFileDirOrName(VideoRequest.path, Static.VideoPath, VideoRequest.oldname, VideoRequest.newname));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

}
